package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.StockProducto;

public class StockProductoDAO {
	
	private static StockProductoDAO instancia = null;
	private static SessionFactory sf = null;

	public static StockProductoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new StockProductoDAO();
		} 
		return instancia;
	}
	
	public void guardarStockProducto(StockProducto sp){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(sp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarStockProducto(StockProducto sp){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(sp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarStockProducto(StockProducto sp){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(sp);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<StockProducto> obtenerStockProductos(){
		Session session = sf.openSession();
		Query q = session.createQuery("from StockProductos");
		List<StockProducto> stockProductos = q.list();
		session.close();
		return stockProductos;
	}
}




